/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.proportional;

import control.CBacheca;
import control.CGestioneRubrica;
import customGuiControl.ImagePanel;
import customGuiControl.Listner.wizardRollOverListner;
import customGuiControl.RoundedPanel2;
import customGuiControl.roundButton;
import customGuiControl.roundedPanel;
import entity.xml.EUtente;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import org.jdesktop.swingx.StackLayout;

/**
 *
 * @author Ennio
 */
public class PContattaPersona implements MouseListener {
    
    private static PContattaPersona istance= null;
    private JPanel panel;
    private JPanel panelInside;
    private ArrayList<EUtente> receiverList= null;
    private JPanel pan;
    private JPanel cercaBacheca;
    private RoundedPanel2 menuSotto;
    private String precTooltipText= "";
    
    
    private PContattaPersona(){
        this.panel=new JPanel(new relativeMarginLayout(15f,5f, 5f, 5f));
        this.panelInside=new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.panel.add(this.panelInside);
        this.panel.setOpaque(false);
        this.panelInside.setOpaque(false);
        
        this.setPanelContattaPersona();
        
    }
    
    public JPanel getPanel() {
        return panel;
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }
    
    
    
    public static PContattaPersona getInstance(){
    
        if(istance==null) istance= new PContattaPersona();
        return istance;
        
    }
    
    public void setPanelContattaPersona(){
        
        this.receiverList= new ArrayList<EUtente>();
       
        
        this.printContacts();
        
    }
    
    public void printContacts(){
        
        ArrayList<EUtente> rubricaUtente= new ArrayList<EUtente>();
        rubricaUtente= CGestioneRubrica.getIstance().getRubricaByUser(CBacheca.getIstance().getCurrUser());
        for(int i=0; i<rubricaUtente.size(); i++){
            PanelRubricaContact userPanel= PGestioneRubrica.getInstance().printUserInRubrica(rubricaUtente.get(i));
            userPanel.addMouseListener(this);
            this.panelInside.add(userPanel);
        }
        
    }
    
    
    public RoundedPanel2 getMenuSotto(gui instance){
         if(this.menuSotto==null)refreshMenuSotto(instance);
         return this.menuSotto;
     }
    public void refreshMenuSotto(gui instance){
        
        /*roundedPanel rp= new roundedPanel(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addFindButton(gbc, rp, this, instance);
        
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=2;
        gbc.gridy=0;
        gbc.weightx=0.99;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addChooseReceiverButton(gbc, rp, this, instance);
        
        gbc.gridx=3;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addAnnullaButton(gbc, rp, this, instance);
        
        this.menuSotto= rp;
        
    }
    
    public void mouseClicked(MouseEvent e) {
        
        String sourceClass= e.getSource().getClass().toString();
        
        if( sourceClass.contains("ImagePanel")){
            
            String pathImg= ((ImagePanel) e.getSource()).getPathImg();
            
            if(pathImg.contains("cerca")){
                JOptionPane.showMessageDialog(null, "Cliccato cerca");
            }
            else if(pathImg.contains("destinatario")){
                
                if(this.receiverList.size()==0){
                    JOptionPane.showMessageDialog(null, "Selezionare almeno un destinatario");
                }
                else{
                    
                    PComponiMessaggio.getInstance().refreshPanel(receiverList);
                    gui.getIstance().buttonClicked(gui._componiMessaggio);
                }
                
            }
            else if(pathImg.contains("annulla")){
                JOptionPane.showMessageDialog(null, "Cliccato annulla");
            }
            
        }
        else{
            
            PanelRubricaContact panelClickedUser= (PanelRubricaContact)e.getComponent();
            String nickClickedUser= panelClickedUser.getNicknameUser().getText();
            
            
            EUtente clickedUser= CGestioneRubrica.getIstance().getContactBacheca(nickClickedUser);
            this.selectContact((PanelRubricaContact) e.getComponent(), clickedUser);
            
        }
        
    }
    public void selectContact(PanelRubricaContact panel, EUtente clickedUser){
        
        if(this.receiverList.contains(clickedUser)){
            panel.signAsUnchecked();
            this.receiverList.remove(clickedUser);
        }
        else{
            panel.signAsChecked();
            this.receiverList.add(clickedUser);
        }
        
    }
    
    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        
        if(e.getSource().getClass().toString().contains("PanelRubricaContact")){
            this.precTooltipText= gui.getIstance().getInfoTextPane().getText();
            gui.getIstance().getInfoTextPane().setText("Clicca per selezionare/deselezionare come destinatario");
        }
                
    }

    public void mouseExited(MouseEvent e) {
    
        if(e.getSource().getClass().toString().contains("PanelRubricaContact")){
            gui.getIstance().getInfoTextPane().setText(this.precTooltipText);
        }

    }

    public ArrayList<EUtente> getReceiverList() {
        return receiverList;
    }

    public void setReceiverList(ArrayList<EUtente> receiverList) {
        this.receiverList = receiverList;
    }
    
    
}
